package engine.services.impl;

import org.springframework.core.io.Resource;

import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailService.Message;
import com.google.appengine.api.mail.MailServiceFactory;

import engine.content.ContentTypes;
import engine.content.IContent;
import engine.mail.GoogleMailMessageWrapper;
import engine.mail.IMailMessage;
import engine.services.IMailSender;

@SuppressWarnings("rawtypes")
public class GoogleMailSenderImpl implements IMailSender {



	@Override
	public IMailMessage createMessage() {
		@SuppressWarnings("unused")
		MailService mailService = MailServiceFactory.getMailService();
		MailService.Message message = new MailService.Message();
		return new GoogleMailMessageWrapper(message);
	}


	@Override
	public void send(IMailMessage mm) throws Exception {
		MailServiceFactory.getMailService().send((Message)mm.getMessage());			
	}


	@Override
	public void addAttachment(IContent content, Resource res, IMailMessage message, String fileName, String fileId)
			throws Exception {
		if (content.getType() != ContentTypes.MEMORY) {
			throw new RuntimeException("Only in memory content is allowed for attachments");
		}
		MailService.Attachment attachment =
		        new MailService.Attachment(fileName,(byte[])content.getContent());
		((Message)message.getMessage()).setAttachments(attachment);
		
	}

}
